//
//  PywPayHeadView.m
//  PywSdk
//
//  Created by Jade on 16/7/21.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywPayHeadView.h"
#import "PywMasonry.h"
#import "UIImage+Pyw.h"
#import "PywPlatformMain.h"
#import "PywAppInfo.h"
@interface PywPayHeadView()
{
    CGFloat _height;
}
@property (nonatomic,strong) PywPayInfoModel *payInfo;
@property (nonatomic,strong) UIView *infoView;
@property (nonatomic,strong) UILabel *infoNameLabel;
@property (nonatomic,strong) UILabel *infoValueLabel;


@property (nonatomic,strong) UIButton *couponButton;
@property (nonatomic,strong) UILabel *couponLabel;

/**
 *  点击代金券回调
 */
@property (nonatomic,copy) SelectCoupon selectCoupon;

@end
@implementation PywPayHeadView

- (instancetype)initWithPayInfo:(PywPayInfoModel *)payInfo
                   selectCoupon:(SelectCoupon)selectCoupon
{
    _height = [PywAppInfo sharedAppInfo].is_applepay?90:145;
    CGRect frame = CGRectMake(0, 0, SCREEN_WIDTH, _height);
    self = [super initWithFrame:frame];
    if (self) {
        self.payInfo = payInfo;
        self.selectCoupon = selectCoupon;
        [self setUp];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.infoView mas_updateConstraints:^(PywMASConstraintMaker *make) {
        make.top.left.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(80);
    }];
}

#pragma mark - setUp
- (void)setUp
{
    self.backgroundColor = RGB(232, 232, 232, 1);
    [self addSubview:self.infoView];
    [self.infoView addSubview:self.infoNameLabel];
    [self.infoView addSubview:self.infoValueLabel];
    
    WEAK_SELF;
    [self.infoView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.top.left.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(80);
    }];

    
    [self.infoNameLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.left.equalTo(strongSelf.infoView.mas_left).offset(10);
        make.centerY.equalTo(strongSelf.infoView.mas_centerY);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(50);
    }];
    
    
    [self.infoValueLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.top.equalTo(strongSelf.infoNameLabel.mas_top);
        make.left.equalTo(strongSelf.infoNameLabel.mas_right);
        make.right.equalTo(strongSelf.infoView.mas_right).offset(-10);
        make.height.mas_equalTo(strongSelf.infoNameLabel.mas_height);
    }];
    
    
    if (![PywAppInfo sharedAppInfo].is_applepay) {
        [self addSubview:self.couponButton];
        [self.couponButton mas_makeConstraints:^(PywMASConstraintMaker *make) {
            STRONG_SELF(weakSelf);
            make.top.equalTo(strongSelf.infoView.mas_bottom).offset(10);
            make.left.right.equalTo(strongSelf);
            make.height.mas_equalTo(45);
        }];
    }
 
}

#pragma mark - Getter
- (UIView *)infoView
{
    if (!_infoView) {
        _infoView = [[UIView alloc] init];
        _infoView.backgroundColor = [UIColor whiteColor];
    }
    return _infoView;
}

- (UILabel *)infoNameLabel
{
    if (!_infoNameLabel) {
        _infoNameLabel = [[UILabel alloc] init];
        _infoNameLabel.font = [UIFont systemFontOfSize:15.f];
        _infoNameLabel.numberOfLines = 0;
        _infoNameLabel.textColor = UIColorFromRGB(0xAAAAAA);
        
        NSString *infoMessage = [NSString stringWithFormat:@"商品名称：\n订单金额："];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:infoMessage];
        NSMutableParagraphStyle *parahraphStyle = [[NSMutableParagraphStyle alloc] init];
        [parahraphStyle setLineSpacing:5]; // 设置行间距
        [attributedString addAttribute:NSParagraphStyleAttributeName value:parahraphStyle range:NSMakeRange(0, infoMessage.length)];
        _infoNameLabel.attributedText = attributedString;
        [_infoNameLabel sizeToFit];
    }
    return _infoNameLabel;
}


- (UILabel *)infoValueLabel
{
    if (!_infoValueLabel) {
        _infoValueLabel = [[UILabel alloc] init];
        _infoValueLabel.font = [UIFont systemFontOfSize:15.f];
        _infoValueLabel.numberOfLines = 2;
        NSString *productName = self.payInfo.productDescript;
        NSString *orderAmounct = [NSString stringWithFormat:@"%.2f", self.payInfo.amount];
        
        NSString *infoMessage = [NSString stringWithFormat:@"%@\n￥%@", productName, orderAmounct];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:infoMessage];
        NSMutableParagraphStyle *parahraphStyle = [[NSMutableParagraphStyle alloc] init];
        parahraphStyle.alignment = NSTextAlignmentLeft;
        [parahraphStyle setLineSpacing:5]; // 设置行间距
        [attributedString addAttribute:NSParagraphStyleAttributeName value:parahraphStyle range:NSMakeRange(0, infoMessage.length)];
        _infoValueLabel.attributedText = attributedString;
        [_infoValueLabel sizeToFit];
    }
    return _infoValueLabel;
}

- (UIButton *)couponButton
{
    if (!_couponButton) {
        _couponButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _couponButton.backgroundColor = [UIColor whiteColor];
        [_couponButton addTarget:self
                          action:@selector(selectCoupon:)
                forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:15.f];
        titleLabel.text = @"代金券";
        [_couponButton addSubview:titleLabel];
        UIImageView *iconImgView = [[UIImageView alloc] init];
        iconImgView.image = [UIImage imageWithName:@"icon_next_normal"];
        [_couponButton addSubview:iconImgView];
        [_couponButton addSubview:self.couponLabel];
        
        [titleLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.centerY.equalTo(_couponButton.mas_centerY);
            make.left.equalTo(_couponButton.mas_left).offset(10);
        }];
        [iconImgView mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.right.equalTo(_couponButton.mas_right);
            make.centerY.equalTo(_couponButton.mas_centerY);
        }];
        [self.couponLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.right.equalTo(iconImgView.mas_left).offset(-5);
            make.centerY.equalTo(iconImgView.mas_centerY);
        }];
        
        
    }
    return _couponButton;
}

- (UILabel *)couponLabel
{
    if (!_couponLabel) {
        _couponLabel = [[UILabel alloc] init];
        _couponLabel.font = [UIFont systemFontOfSize:13.f];
        _couponLabel.textColor = RGB(250, 65, 72, 1);
        _couponLabel.text = @"选择代金券";
        _couponLabel.textAlignment = NSTextAlignmentRight;
    }
    return _couponLabel;
}


#pragma mark - Setter

- (void)setCouponAmount:(CGFloat)couponAmount
{
    _couponAmount = couponAmount;
    self.couponLabel.text = [NSString stringWithFormat:@"已抵扣:￥%.2f",couponAmount];
}
/**
 *  显示无可用的代金券显示
 */
- (void)updateCouponPrompt
{
    self.couponLabel.text = @"无可用";
}

#pragma mark - Actions
/**
 *  选择代金券
 *
 *  @param sender <#sender description#>
 */
- (void)selectCoupon:(UIButton *)sender
{
    if (self.selectCoupon) {
        self.selectCoupon();
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


@end
